//
//  MainTabBarController.m
//  Weibo
//
//  Created by student on 16/4/22.
//  Copyright © 2016年 Afash. All rights reserved.
//

#import "MainTabBarController.h"
#import "ProfileTableViewController.h"
#import "DiscoverTableViewController.h"
#import "MessageTableViewController.h"
#import "HomeTableViewController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //1.设置当前控制器tabBar的颜色
    self.tabBar.tintColor = [UIColor orangeColor];
   
    //2.添加子视图控制器
    [self addChidViewControllers];
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //添加中间按钮
    [self addComposeBtn];
}
-(void)addChidViewControllers
{
    //改成读取JSON，根据数据创建视图控制器
    NSString *path = [[NSBundle mainBundle]pathForResource:@"MainVCSettings" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    for(NSDictionary *dict in arr)
    {
        NSLog(@"%@ %@ %@ ",dict[@"vcName"],dict[@"title"],dict[@"imageName"]);
        [self addChildViewController:dict[@"vcName"] imageName:dict[@"imageName"] title:dict[@"title"]];
        
    }
    
    
        //TabBarController添加子视图控制器
//          [self addChildViewController:[[HomeTableViewController alloc]init] imageName:@"tabbar_home" title:@"首页"];
//          [self addChildViewController:[[MessageTableViewController alloc]init] imageName:@"tabbar_message_center" title:@"消息"];
//          [self addChildViewController:[[UITableViewController alloc]init] imageName:@"" title:@""];
//          [self addChildViewController:[[DiscoverTableViewController alloc]init] imageName:@"tabbar_discover" title:@"发现"];
//          [self addChildViewController:[[ProfileTableViewController alloc]init] imageName:@"tabbar_profile" title:@"我"];
}
//封装
//-(void)addChildViewController:(UIViewController *)vc
//                    imageName:(NSString *)imageName
//                        title:(NSString *)title
-(void)addChildViewController:(NSString *)vcName
                    imageName:(NSString *)imageName
                        title:(NSString *)title
{
    UIViewController *vc = [[NSClassFromString(vcName) alloc]init];
    //1.设置其tabbarItem上的图标
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]];
    //2.设置其tabbarItem上的文字
    //    vc.tabBarItem.title = title;
    //3.把首页嵌入到一个导航栏控制器
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    //4.设置导航栏上的标题
    //    vc.navigationItem.title = @"首页";
    //注释：2.和4. 可以使用以下一行替代；
    vc.title = title;
    
    //5.将导航栏控制器添加到tabBar
    [self addChildViewController:navi];
}
//添加中间的按钮
-(void)addComposeBtn
{
    UIButton *btn = [[UIButton alloc]init];
   
    //图片
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
   
    //背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //设置尺寸位置
    int width = [UIScreen mainScreen].bounds.size.width / self.viewControllers.count;//按钮宽度
    btn.frame = CGRectMake(width*2, 0, width, 49);
   
    //按钮添加事件
    [btn addTarget:self action:@selector(composeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    
    //添加到tabbar上
    [self.tabBar addSubview:btn];
}
-(void)composeBtnPressed
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
